﻿using SqlSugar;
using System.Reflection;

namespace flowerWebApi
{
    public class FlowerContext
    {
        public static SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { 
            ConnectionString = @"Data Source=DESKTOP-RKPI4AD\SQLEXPRESS;Initial Catalog=Flower;User ID=sa;pwd=sa",
            DbType = DbType.SqlServer, //数据库类型
            IsAutoCloseConnection = true //不设成true要手动close，自动关闭
        });

        public static void InitDataBase()
        {
            //初始化数据库，如果不存在则创建数据库
            db.DbMaintenance.CreateDatabase();

            //创建表
            string nspace = "Model";
            Type[] ass = Assembly.LoadFrom("bin/Debug/net6.0/Model.dll").GetTypes().Where(t => t.Namespace == nspace).ToArray();
            db.CodeFirst.SetStringDefaultLength(200).InitTables(ass);
        }
    }
}
